## !/bin/sh

#脚本路径
command_path=$(cd "$(dirname "$0")";pwd)

# 工程名
project_name=央企境外检测

#Scheme名称
project_scheme=jwjc

#编译模式
build_type=Debug

# workspace名
project_workspace="/Users/zhangyuepeng/work/react-native/jwjc/ios/jwjc.xcworkspace"

# archive_path        eg:$command_path/$project_name.xcarchive
archive_path=$command_path/jwjc/$project_name.xcarchive

# ipa文件存放路径        eg:$command_path/$project_name.ipa
export_ipa_path=$command_path/jwjc

# exportOptions plist       eg:$command_path/ExportOptions.plist
# 这个文件可以从以前打包到处的ipa文件目录查找并修改证书及关键配置
export_options_plist=$command_path/jwjc/ExportOptions.plist

echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"
echo "请选择您要导出ipa类型:\n\t 1:enterprise \n\t 2:app-store \n\t 3:ad-hoc\n \t 4:develop\n\t"
echo "\t 0:退出"
echo "\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"

read number
while  [[ $number != 0 ]] && [[ $number != 1 ]] && [[ $number != 2 ]] && [[ $number != 3 ]]; do
	echo "\033[1;31m请输入1或者2或者3或者4\033[0m\n"
	echo "请选择您要导出ipa类型:\n \t1:app-store\n \t2:ad-hoc\n \t3:develop"
	read number
done
if [[ $number == 0 ]]; then
    exit 8
fi

#
# react native start
#
# 同步修改iOS build 和 version
./version.ios.sh
# 生产jsbundle
npm run bundle-ios
# react native end
#

if [[ $number == 1 ]]; then
    build_type=Release
    export_options_plist=$command_path/jwjc/ExportOptions.plist
elif [[ $number == 2 ]]; then
    build_type=Release
    export_options_plist=$command_path/jwjc/ExportOptions_appstore.plist
elif [[ $number == 3 ]]; then
    build_type=Release
    export_options_plist=$command_path/jwjc/ExportOptions_adhoc.plist
else
    build_type=Debug
    export_options_plist=$command_path/jwjc/ExportOptions_dev.plist
fi

echo "///-----------"
echo "/// 正在清理工程"
echo "///-----------"
xcodebuild clean -workspace $project_workspace -scheme $project_scheme -configuration $build_type -quiet || exit

echo "///-----------"
echo "/// 正在编译工程: ${build_type}, ${project_workspace},${project_scheme},$archive_path"
echo "///-----------"
xcodebuild archive -workspace ${project_workspace} -scheme ${project_scheme} -configuration $build_type -archivePath ${archive_path} || exit

echo "///-----------"
echo "/// 开始导出ipa: ${export_ipa_path}"
echo "///-----------"
xcodebuild -exportArchive -archivePath $archive_path -exportPath ${export_ipa_path} -exportOptionsPlist ${export_options_plist} -quiet || exit

# 校验ipa包是否导出
if [[ -e $export_ipa_path/$project_scheme.ipa ]]; then
    echo "///-----------"
    echo "/// ipa包已导出"
    echo "///-----------"
    open $export_ipa_path
fi